<?php
	require_once (dirname(__FILE__)."/../config/Config.inc");
	require_once (dirname(__FILE__)."/../config/api.inc");
	$result['status'] = '0';
	
	//获取验证码流程
	$phone =  getParam('phone','get');
	
	$type =  getParam('type','get');
	
	$loginkey = getParam('loginkey','get');
	
	fileLog(json_encode($_REQUEST));
	
	if(!in_array($type, array('reg','forget','editphone','tixian')))
	{
		$result['msg'] = '非法获取验证码';
		echoJSON($result);
		exit;
	}
	
	//根据类型判断手机号是否已存在
	
	if($type == 'tixian')
	{
		require_once (ROOTDIR."api/my/load.php");
		
		$phone = $userinfo['loginname'];
	}
	
	if($type == 'editpwd')
	{
		require_once (ROOTDIR."api/my/load.php");
		
		$phone = $userinfo['loginname'];
	}
	
	if($type == 'reg')
	{
		if(empty($phone))
		{
			$result['msg'] = '请输入手机号';
			echoJSON($result);
			exit;
		}
		
		unset($query);
		$query['loginname'] = $phone;
		
		if($userinfo = _SqlTool::getInfo('users', $query))
		{
			$result['msg'] = '手机号已注册';
			echoJSON($result);
			exit;
		}
	}
	
	if($type == 'forget')
	{
		if(empty($phone))
		{
			$result['msg'] = '请输入手机号';
			echoJSON($result);
			exit;
		}
		
		unset($query);
		$query['loginname'] = $phone;
		
		if(!$userinfo = _SqlTool::getInfo('users', $query))
		{
			$result['msg'] = '手机号没有注册';
			echoJSON($result);
			exit;
		}
		else
		{
			if($userinfo['status'] != '1')
			{
				$result['msg'] = '帐号已被禁用';
				echoJSON($result);
				exit;
			}
		}
	}
	
	unset($where);
	
	$where['phone'] = $phone;
	$where['today'] = strtotime(date('Y-m-d'));
	if($uservalid = _User::getUserValidCode($where))
	{
		//验证码发送规则
		if($uservalid['errornums'] >= 5)
		{
			$result['status'] = '0';
			$result['msg'] = '验证码错误次数过多';
			echoJSON($result);
			exit;
		}
		
		if($uservalid['sendnums'] >= 50)
		{
			$result['msg'] = '超出获取最大次数';
			echoJSON($result);
			exit;
		}

		//调用发送短信接口,发送验证码
		$msg['code'] =  $uservalid['code'];
		send_validcode($phone, $msg);
		
		unset($query,$data);
		$query['id'] = $uservalid['id'];
		$data['sendnums'] = $uservalid['sendnums']+1;
		
		_User::setUserValidCode($query, $data);
		
		$result['status'] = '1';
		$result['msg'] = '获取验证码成功';
	}
	else
	{
		$code  = getcode(4);
		
		unset($data);
		
		$data['phone'] = $phone;
		
		$data['code'] = $code;
		
		$data['optime'] = time();
		
		$data['today'] = strtotime(date('Y-m-d'));
		
		$res = _User::addUserValidCode($data);
		
		if(!empty($res))
		{
			//调用发送短信接口,发送验证码//// 修改密码的正在想内容
			$msg['code'] =  $code;
			send_validcode($phone, $msg);
			$result['status'] = '1';
			$result['msg'] = '获取验证码成功';
		}
		else
		{
			$result['msg'] = '获取验证码失败';
		}
	}
	
	echoJSON($result);